<template>
    <div class="app-container">
        <div class="head">
            <img src="@/assets/img/Layout/position.png" />
            <p class="see-block">{{ titleType == 2 ? '其他入库单详情' : '其他入库单新建' }}</p>
        </div>
        <div class="view-container">
            <el-row class="status-title">
                <el-col class="mr20 width100" v-if="form.status == '0'">
                    <el-button
                        class="btn-success width100"
                        type="primary"
                        @click="submitForm()"
                    >确定</el-button>
                </el-col>
            </el-row>
            <el-form ref="form" :model="form" label-width="100px" class="mt30">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="单据编码:" prop="code">
                            <el-input v-model="form.code" placeholder="系统自动生成" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="业务日期:" prop="businessDate">
                            <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model="form.businessDate" suffix-icon="el-icon-date" type="datetime" placeholder="" style="width:100%;" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="closeScan == true">
                        <ul class="scan" style="width:60%;">
                            <li @click="onCloseScanCode" class="close">删除扫码</li>
                        </ul>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="业务部门:" prop="businessUnit">
                            <el-select v-model="form.businessUnit" style="width:100%;" placeholder="请选择" @change="onChangeSave" :disabled="form.status !== '0'">
                                <el-option
                                    v-for="item in businessUnitList"
                                    :key="item.value"
                                    :label="item.value"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="业务类型:" prop="businessType">
                            <el-select v-model="form.businessType" style="width:100%;" placeholder="请选择" @change="onChangeSave" :disabled="form.status !== '0'">
                                <el-option label="其他杂项入库" value="0"></el-option>
                                <el-option label="盘亏单" value="1"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="联系人:" prop="contact">
                            <el-input v-model="form.contact" placeholder="" @blur="onBlurSave" :disabled="form.status !== '0'" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="联系号码:" prop="phone">
                            <el-input v-model="form.phone" placeholder="" @blur="onBlurSave" :disabled="form.status !== '0'" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="16">
                        <el-form-item label="联系地址:" prop="address">
                            <el-input v-model="form.address" placeholder="" @blur="onBlurSave" :disabled="form.status !== '0'" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="入库人:" prop="picker">
                            <el-input v-model="form.picker" placeholder="" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="备注:" prop="remark">
                            <el-input v-model="form.remark" placeholder="" @blur="onBlurSave" :disabled="form.status !== '0'" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <!-- class="input-data" -->
                <input v-model="scanCodeData.code"
                    ref="getFocus"
                    class="input-data"
                    type="password"
                    @keydown.enter="numberEnterDown()"
                    @keyup.enter="numberEnterUp()"
                />
            </el-form>
            <el-row class="detail-item">
                <el-col :span="24">
                    <el-table :data="productData" @row-click="scanCode">
                        <el-table-column label="序号" type="index" align="center" width="50" />
                        <el-table-column :label="item.label" :prop="item.prop" :width="item.width" v-for="(item, index) in columns" :key="index" align="center">
                            <template slot-scope="scope">
                                <template v-if="item.type == '1'">
                                    <el-input v-model="scope.row[item.prop]" placeholder="请输入" disabled="">
                                        <el-button slot="append" icon="el-icon-search" @click="productShow"></el-button>
                                    </el-input>
                                </template>
                                <template v-if="item.type == '3'">
                                    {{ scope.row[item.prop] }}
                                </template>
                                <template v-if="item.type == 'batch'">
                                    <template v-if="scope.row.enableBatches == '0'">
                                        <el-input v-model="scope.row[item.prop]" placeholder="请输入批次号" @click.native.stop="stopScanCode" @blur="onBlurSave" :disabled="scope.row.status !== '0'" />
                                    </template>
                                    <template v-else-if="scope.row.enableBatches == '1'">
                                        <el-input placeholder="未启用批次" disabled />
                                    </template>
                                    <template v-else>
                                        {{ scope.row[item.prop] }}
                                    </template>
                                </template>
                                <template v-if="item.type == 'num'">
                                    <template v-if="scope.row.scanCode == '1'">
                                        <el-input placeholder="请输入数量" v-model="scope.row[item.prop]" @input="(val) => inputNum(val, scope.row)" @click.native.stop="stopScanCode" @blur="onBlurSave" :disabled="scope.row.multiPositionList !== null || scope.row.status !== '0'" />
                                    </template>
                                    <template v-else-if="scope.row.scanCode == '0'">
                                        <el-input placeholder="扫码入库" v-model="scope.row[item.prop]" disabled />
                                    </template>
                                    <template v-else>
                                        {{ scope.row[item.prop] }}
                                    </template>
                                </template>
                                <template v-if="item.type == 'warehouse'">
                                    <el-select v-model="scope.row[item.prop]" placeholder="请选择" style="width:100%;" v-if="scope.row.scanCode == '1'"  @change="val => onSelectWarehouse(val, scope.row)" :disabled="scope.row.productCode == '' || scope.row.status !== '0'">
                                        <el-option v-for="(item,index) of warehouseList" :key="index" :label="item.name" :value="item.warehouseId"></el-option>
                                    </el-select>
                                    <el-select v-model="scope.row[item.prop]" placeholder="请选择" style="width:100%;" v-else @change="val => onSelectWarehouse(val, scope.row)" :disabled="scope.row.endNum !== '' || scope.row.status !== '0'">
                                        <el-option v-for="(item,index) of warehouseList" :key="index" :label="item.name" :value="item.warehouseId"></el-option>
                                    </el-select>
                                </template>
                                <el-input v-model="scope.row[item.prop]" v-if="item.type == 'position'" disabled placeholder="点击搜索按钮搜索" @click.native.stop="stopScanCode">
                                    <el-button slot="append" icon="el-icon-search" @click.stop="onClickWarehouseList(scope.$index, scope.row)"></el-button>
                                </el-input>
                                <template v-if="item.type == 'remark'">
                                    <el-input placeholder="备注" v-model="scope.row[item.prop]" @click.native.stop="stopScanCode" @blur="onBlurSave" :disabled="scope.row.productCode == '' || scope.row.status !== '0'" />
                                </template>
                                <template v-if="item.type == 'del'">
                                    <span class="del" v-if="scope.row.status == '0' && (scope.row.endNum == 0 || scope.row.endNum == '')" @click.stop="deleteRow(scope.row)">删除</span>
                                </template>
                                <template v-if="item.type == 'more'">
                                    <el-tooltip v-if="scope.row.productCode !== '' && (scope.row.warehouse !== '' && scope.row.warehouse !== null) && scope.row.areaId !== null " effect="dark" content="多仓位" placement="bottom">
                                        <span class="el-icon-s-grid more" @click.stop="showMorePosition(scope.row)"></span>
                                    </el-tooltip>
                                </template>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-col>
            </el-row>
        </div>
        <!-- 多仓位 -->
        <el-dialog :visible.sync="morePosition" width="800px" top="100px" append-to-body center :close-on-click-modal='false' :close-on-press-escape='false' :show-close="false">
            <el-row class="mb20" v-if="closeScanMore == true">
                <el-col :span="4">
                    <ul class="scan">
                        <li @click="onCloseScanCode" class="close">删除扫码</li>
                    </ul>
                </el-col>
            </el-row>
            <el-table :data="moreData" @row-click="scanCodeMorePosition">
                <el-table-column label="名称" align="center" prop="productName" width="150" />
                <el-table-column label="实际数量" align="center" prop="endNum" width="120">
                    <template slot-scope="scope">
                        <template v-if="scope.row.scanCode == '0'">
                            <el-input placeholder="扫码入库" v-model="scope.row.endNum" disabled @click.native.stop="stopScanCode" />
                            </template>
                        <template v-else>
                            <el-input placeholder="请输入" v-model="scope.row.endNum" @input="(val) => inputEditNum(val, scope.row)" @blur="(val) => onBlur(val, scope.row)" @click.native.stop="stopScanCode" :disabled="scope.row.status !== '0'" />
                        </template>
                    </template>
                </el-table-column>
                <el-table-column label="仓库" align="center" prop="warehouse">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.warehouse" placeholder="请选择" style="width:100%;" disabled>
                            <el-option v-for="(item,index) of warehouseList" :key="index" :label="item.name" :value="item.warehouseId"></el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column label="仓位" align="center" prop="positionName">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.positionName" disabled placeholder="点击按钮搜索" @click.native.stop="stopScanCode">
                            <el-button slot="append" icon="el-icon-search" @click.stop="morePositionWarehouseList(scope.row, scope.$index)"></el-button>
                        </el-input>
                    </template>
                </el-table-column>
                <el-table-column label="" align="center" width="90px" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <template v-if="scope.row.status == '0'">
                            <span class="el-icon-circle-plus-outline icon-more-position" @click.stop="onMorePositionAdd(scope.row)"></span>
                            <span class="el-icon-circle-close icon-more-position" @click.stop="onMorePositionDelete(scope.row)"></span>
                        </template>
                    </template>
                </el-table-column>
            </el-table>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" class="btn-success width100" @click="onHideMorePosition">关闭</el-button>
            </div>
        </el-dialog>
        <!-- 弹出数据 -->
        <el-dialog :title="title" :visible.sync="open" width="70%" top="50px" append-to-body center :before-close="handleClose">
            <el-form :model="queryParams" ref="queryForm" :inline="true" @submit.native.prevent>
                <el-form-item :label="label" prop="searchValue">
                <el-input
                    v-model="queryParams.searchValue"
                    placeholder="请输入名称或编码检索"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
                />
                </el-form-item>
                <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
                </el-form-item>
            </el-form>
            <el-table v-loading="loading" :data="dataList" @row-click="onSelectData" :default-sort="{prop: 'date', order: 'descending'}" border>
                <el-table-column label="序号" type="index" align="center" width="50">
                <template slot-scope="scope">
                    <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
                </template>
                </el-table-column>
                <el-table-column label="SKU编码" align="center" prop="specificationCode" :show-overflow-tooltip="true" width="150" sortable />
                <el-table-column label="SKU名称" align="center" prop="specificationName" :show-overflow-tooltip="true" width="250" />
                <el-table-column label="单位" align="center" prop="unit" :show-overflow-tooltip="true" />
                <el-table-column label="是否启用扫码" align="center" prop="isSerial" width="100">
                    <template slot-scope="scope">
                    <div v-if="scope.row.isSerial == '0'" style="color:#13C2C2">启用</div>
                    <div v-if="scope.row.isSerial == '1'" style="color:#52C41A">禁用</div>
                    </template>
                </el-table-column>
                <el-table-column label="是否启用批次" align="center" prop="enableBatches" width="100">
                    <template slot-scope="scope">
                    <div v-if="scope.row.enableBatches == '0'" style="color:#13C2C2">启用</div>
                    <div v-if="scope.row.enableBatches == '1'" style="color:#52C41A">禁用</div>
                    </template>
                </el-table-column>
                <el-table-column label="审核状态" align="center" prop="approvalStatus">
                    <template slot-scope="scope">
                    <div v-if="scope.row.approvalStatus == 0" style="color:#13C2C2">草稿</div>
                    <div v-if="scope.row.approvalStatus == 1" style="color:#13C2C2">待审核</div>
                    <div v-if="scope.row.approvalStatus == 2" style="color:#52C41A">已审核</div>
                    <div v-if="scope.row.approvalStatus == 3" style="color:#fd562c">拒绝</div>
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center" prop="productStatus">
                    <template slot-scope="scope">
                    <div v-if="scope.row.productStatus == 0" style="color:#13C2C2">禁用</div>
                    <div v-if="scope.row.productStatus == 1" style="color:#52C41A">正常</div>
                    </template>
                </el-table-column>
            </el-table>
            <pagination
                v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
            />
        </el-dialog>
    </div>
</template>

<script>
import { otherStorageAdd, otherStorageEdit, otherStorageDetail, otherStorageDelete, morePosition, morePositionAdd, morePositionAddSingle, morePositionEdit, morePositionDelete, morePositionDeleteSingle, scanCode, scanCodeDelete, confirmStorage, otherWarehouseEdit, morePositionUpdate } from "@/api/system/warehouse/otherStorage"
import { queryWarehouse } from "@/api/system/warehouse/warehouse"
import { listProduct} from "@/api/system/basicdata/basicdata"
export default {
    name: "add",
    data() {
        return {
            id: '',
            form: {
                code: '', // 单据编码
                businessDate: '', // 业务日期
                businessUnit: '', // 业务部门
                businessType: '', // 业务类型
                // receivingWarehouse: '', // 接收仓库
                remark: '', // 备注
                picker: '', // 入库人
                // qualityInspectorName: '', // 质检员
                contact: '', // 联系人
                phone: '', // 联系电话
                address: '', // 联系地址
                detailList: [], // 产品数据
                status: '0',
            },
            // 部门列表
            businessUnitList: [{
                value: '采购部'
            },{
                value: '销售部'
            }],
            warehouseList: [], // 仓库列表
            // 表头
            columns: [{
                prop: "warehouse",
                label: "仓库",
                width: 130,
                type: 'warehouse'
            },{
                prop: "productCode",
                label: "sku编码",
                width: 210,
                type: '1'
            },{
                prop: "productName",
                label: "SKU名称",
                width: 250,
                type: '1'
            },{
                prop: "specification",
                label: "规格",
                width: 150,
                type: '3'
            },{
                prop: "batchNumber",
                label: "批次号",
                width: 150,
                type: 'batch'
            },{
                prop: "unit",
                label: "单位",
                width: 100,
                type: '3'
            },{
                prop: "endNum",
                label: "入库数量",
                width: 120,
                type: 'num'
            },
            {
                prop: "positionName",
                label: "仓位",
                width: 200,
                type: 'position'
            },
            {
                prop: "remark",
                label: "备注",
                width: 200,
                type: 'remark'
            },{
                label: "",
                type: 'del',
                width: 80
            },{
                label: "",
                width: 50,
                type: 'more'
            }
            ],
            productData: [{
                productId: '', // 物料id(SKU)
                productCode: '', // 物料编码(SKU)
                productName: '', // 物料名称(SKU)
                specification: '', // 规格
                batchNumber: '', // 批次号
                unit: '', // 单位
                endNum: '', // 入库数量
                warehouse: '', // 仓库
                areaId: '', // 区域id
                areaName: '', // 区域名称
                positionId: '', // 仓位id
                positionName: '', // 仓位名称
                enableBatches: '', // 是否启用批次
                isSerial: '', // 是否启用扫码
                remark: ''
            }],
            titleType: 1, // 1 新建 2 查看
            // 产品数据
            dataList:[],
            open: false,
            title: '',
            label: '',
            loading: true,
            total: 0,
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                searchValue: undefined,
                approvalStatus: 2,
                productStatus: 1,
            },
            closeScan: false, // 删除扫码
            type: '', // 1 扫码入库 2 删除扫码
            scanCodeData: {
                code: '',
                warehouse: '',
                areaId: '',
                areaName: '',
                positionId: '',
                positionName: '',
                otherInboundDetailId: ''
            },
            morePosition: false,
            moreData: [], // 多仓位数据
            morePositionData: '',
            closeScanMore: false,
            morePositionType: ''
        }
    },
    created(){
        // console.log(this.$store.state)
        // 获取用户信息
        let userInfo = this.$store.state.user.users
        this.form.picker = userInfo.userName
        // 获取数据
        if(this.$route.query.data && this.$route.query.data !== '[object Object]'){
            this.id = this.$route.query.data.id
            this.form = this.$route.query.data
            this.productData = this.$route.query.data.detailList
            if(this.$route.query.id){
                this.$router.push({ query: {id: this.id} })
            }else{
                this.$router.push({ query: {} })
            }
            this.getOtherStorage()
        }
        if(this.$route.query.morePosition && this.$route.query.morePosition !== '[object Object]'){
            this.id = this.$route.query.form.id
            this.form = this.$route.query.form
            this.productData = this.$route.query.form.detailList
            let data = this.$route.query.morePosition
            let index = this.$route.query.index
            morePosition(data.businessId).then(res => {
                if(res.code == 200){
                    this.morePosition = true
                    this.moreData = res.rows
                    this.moreData[index] = data
                    morePositionUpdate(this.moreData[index]).then(res => {
                        if(res.code == 200){
                            this.getDetailData()
                        }else{
                            morePosition(data.businessId).then(res => {
                                this.morePosition = true
                                res.rows.forEach(i => {
                                    if(i.endNum == 0 || i.endNum == null){
                                        i.endNum = ''
                                    }
                                })
                                this.moreData = res.rows
                            })
                            this.$modal.msgError(res.msg)
                        }
                    })
                    if(this.$route.query.id){
                        this.$router.push({ query: {id: this.id} })
                    }else{
                        this.$router.push({ query: {} })
                    }
                }
            })
        }
        if(this.$route.query.id){
            this.id = this.$route.query.id
            this.titleType = 2
            this.getDetailData()
        }
        this.getWarehouseList()
    },
    methods: {
        // 获取详情
        getDetailData(){
            otherStorageDetail(this.id).then(res => {
                this.form = res.data
                if(res.data.detailList == null){
                    this.productData = []
                }else{
                    this.productData = res.data.detailList
                    res.data.detailList.forEach(item => {
                        if(item.endNum == 0 || item.endNum == null){
                            item.endNum = ''
                        }
                    })
                }
                if(res.data.status == 0){
                    this.addProduct()
                }
            })
        },
        // 获取仓库列表
        getWarehouseList(){
            queryWarehouse().then(res => {
                if(res.code == 200){
                    this.warehouseList = res.data
                }
            })
        },
        // 产品数据显示
        productShow(){
            if(this.form.status == '0'){
                if(this.form.contact !== '' && this.form.contact !== null){
                    this.open = true
                    this.title = '产品'
                    this.label = '搜索'
                    this.dataList = []
                    this.getList()
                }else{
                    this.$modal.msgError('联系人不能为空！')
                }
            }
        },
        // 获取弹窗数据
        getList(){
            this.loading = true
            listProduct(this.addDateRange(this.queryParams)).then(res => {
                this.dataList = res.rows
                this.total = res.total
                this.loading = false
            })
        },
        // 搜索按钮
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        // 重置按钮
        resetQuery() {
            this.queryParams.searchValue = ''
            this.handleQuery()
        },
        // 选中数据
        onSelectData(row){
            // 产品选择
            let obj = ''
            obj = {
                otherInboundOrderId: this.id,
                productBasicId: row.productId, // 产品Id
                productBasicCode: row.productCode, // 产品编码
                productBasicName: row.productName, // 产品名称
                productId: row.productSpecificationsId, // sku产品Id
                productCode: row.specificationCode, // sku产品编码
                productName: row.specificationName, // sku产品名称
                specification: row.value1 + row.value2, // 规格
                unit: row.unit, // 单位
                enableBatches: row.enableBatches, // 是否启用批次
                scanCode: row.isSerial, // 是否启用批次
                remark: '', // 备注
            }
            // 判断编码是否重复
            let arr = this.productData.filter(item => item.productCode !== '')
            console.log(arr)
            this.productData = arr
            if(arr && arr.length > 0){
                let index = this.productData.findIndex(item => item.productCode == row.specificationCode)
                if(index > -1){
                    this.$modal.msgError('产品已存在，请重新选择！')
                    this.getDetailData()
                }else{
                    this.productData.push(obj)
                    this.getOtherStorage()
                }
            }else{
                this.productData.push(obj)
                this.getOtherStorage()
            }
            this.open = false
            this.title = ''
            this.label = ''
            this.queryParams.searchValue = undefined
            this.handleQuery()
            this.dataList = []
        },
        // 关闭弹窗
        handleClose(){
            this.open = false
            this.title = ''
            this.label = ''
            this.queryParams.searchValue = undefined
            this.handleQuery()
            this.dataList = []
        },
        // 阻止事件
        stopScanCode(){},
        // 数量验证
        inputNum(val, row){
            if(row.warehouse !== null && row.warehouse !== ''){
                // 当前选中仓库对应仓库列表的数据
                let isWarehouse = this.warehouseList.find(item => item.warehouseId == row.warehouse)
                // 判断是否启用仓位管理
                if(isWarehouse.openPosition == '0'){
                    // console.log(row)
                    if(row.areaName !== null && row.areaName !== ''){
                        if(val == '' || val == undefined){
                            this.$modal.msgError('数量不能为空!')
                            row.endNum = ''
                        }else{
                            if(/^[1-9]\d*$|^0$/.test(val) == false){
                                this.$modal.msgError('数量必须是数字！')
                                row.endNum = ''
                                return false
                            }else{
                                if(JSON.parse(val) <= 0){
                                    this.$modal.msgError('数量必须大于0!')
                                    row.endNum = ''
                                }
                            }
                        }
                    }else{
                        row.endNum = ''
                        this.$modal.msgError('请选择仓位!')
                    }
                }else{
                    if(val == '' || val == undefined){
                        this.$modal.msgError('数量不能为空!')
                        row.endNum = ''
                    }else{
                        if(/^[1-9]\d*$|^0$/.test(val) == false){
                            this.$modal.msgError('数量必须是数字！')
                            row.endNum = ''
                            return false
                        }else{
                            if(JSON.parse(val) <= 0){
                                this.$modal.msgError('数量必须大于0!')
                                row.endNum = ''
                            }
                        }
                    }
                }
            }else{
                row.endNum = ''
                this.$modal.msgError('请选择仓库!')
            }
        },
        // 产品仓库选择
        onSelectWarehouse(val, row){
            // console.log(row)
            otherWarehouseEdit({id: row.id, warehouse: row.warehouse, endNum: 0, updateWarehouse: 1 }).then(res => {
                if(res.code == 200){
                    this.getDetailData()
                }
            })
            // 当前选中仓库对应仓库列表的数据
            let isWarehouse = this.warehouseList.find(item => item.warehouseId == row.warehouse)
            if(isWarehouse.openPosition !== '0'){
                morePosition(row.id).then(res => {
                    // 判断多仓位
                    if(res.rows.length !== 0){
                        morePositionDelete({businessId: row.id, businessType: 2}).then(res => {
                            if(res.code == 200){
                                // this.$modal.msgError('未启用仓位管理！')
                            }
                        })
                    }
                })
            }
        },
        // 跳转仓库列表页面
        onClickWarehouseList(index, row){
            if(row.status == '0'){
                if(row.warehouse !== '' && row.warehouse !== null){
                    // 当前选中仓库对应仓库列表的数据
                    let isWarehouse = this.warehouseList.find(item => item.warehouseId == row.warehouse)
                    // 判断是否启用仓位管理
                    if(isWarehouse.openPosition == '0'){
                        if(row.endNum !== ''){
                            this.$modal.msgError('入库数量已存在，不能编辑!')
                        }else{
                            morePosition(row.id).then(res => {
                                if(res.rows.length > 0){
                                    this.$modal.msgError('已存在多仓位，请去多仓位编辑!')
                                }else{
                                    if(this.$route.query.id){
                                        this.$router.push({ path:"/warehouseManagement/warehouse/list", query:{ id: this.$route.query.id, type: 'otherStorage', index: index, data: this.form }})
                                    }else{
                                        this.$router.push({ path:"/warehouseManagement/warehouse/list", query:{ type: 'otherStorage', index: index, data: this.form }})
                                    }
                                }
                            })
                        }
                    }else{
                        this.$modal.msgError('未启用仓位管理！')
                    }
                }else{
                    this.$modal.msgError('请选择仓库！')
                }
            }
        },
        // 单仓位扫码
        scanCode(row){
            if(row.status == 0){
                if(row.warehouse !== null && row.productCode !== ''){
                    // console.log(row)
                    // 当前选中仓库对应仓库列表的数据
                    let isWarehouse = this.warehouseList.find(item => item.warehouseId == row.warehouse)
                    morePosition(row.id).then(res => {
                        if(res.code == 200){
                            //  判断是否启用扫码
                            if(row.scanCode == '0'){
                                // 判断是否启用仓位管理
                                if(isWarehouse.openPosition == '0'){
                                    // 判断是否选择仓位
                                    if(row.areaName !== null && row.areaName !== ''){
                                        this.getScanCode(row)
                                    }else{
                                        this.closeScan = false
                                        this.$modal.msgError('请选择仓库、仓位!')
                                    }
                                }else{
                                    this.getScanCode(row)
                                }
                            }else{
                                this.closeScan = false
                                this.scanCodeData = {}
                                this.$modal.msgError('未启用扫码!')
                            }
                        }
                    })
                }
            }
        },
        // 多仓位扫码
        scanCodeMorePosition(row){
            // console.log(row)
            //  判断是否启用扫码
            if(this.status == '0'){
                if(row.scanCode == '0'){
                    // 判断是否选择仓位
                    if(row.areaName !== null && row.areaName !== ''){
                        this.getScanCodeMore(row)
                    }else{
                        this.closeScan = false
                        this.$modal.msgError('请选择仓位!')
                    }
                }else{
                    this.closeScanMore = false
                    this.scanCodeData = {}
                    this.$modal.msgError('未启用扫码!')
                }
            }
        },
        // 开始扫码
        getScanCode(row){
            this.$refs.getFocus.focus()
            this.closeScan = true
            this.type = '1'
            this.scanCodeData = {
                otherInboundDetailId: row.id,
                warehouse: row.warehouse,
                areaId: row.areaId,
                areaName: row.areaName,
                positionId: row.positionId,
                positionName: row.positionName
            }
            this.$modal.msgSuccess("开始扫码！")
        },
        // 多仓位开始扫码
        getScanCodeMore(row){
            this.$refs.getFocus.focus()
            this.closeScanMore = true
            this.type = '1'
            this.morePositionType = '1'
            this.scanCodeData = {
                otherInboundDetailId: row.businessId,
                warehouse: row.warehouse,
                areaId: row.areaId,
                areaName: row.areaName,
                positionId: row.positionId,
                positionName: row.positionName
            }
            this.$modal.msgSuccess("开始扫码！")
        },
        // 删除扫码
        onCloseScanCode(){
            this.$refs.getFocus.focus()
            this.$modal.msgSuccess("删除扫码")
            this.scanCodeData.code = ''
            this.type = '2'
            this.morePositionType = '1'
            console.log(this.scanCodeData)
        },
        numberEnterDown() {
            this.keyDownTime = new Date().getTime()
        },
        numberEnterUp() {
            this.keyUpTime = new Date().getTime()
            if (this.keyUpTime - this.keyDownTime < 30) {
                // 扫码枪触发
                let newCode = this.common.codeRule(this.scanCodeData.code)
                // let code = this.scanCodeData.code.split("=")
                // let newCode = code[code.length-1]
                this.scanCodeData.code = newCode
                if(newCode.length == 13 || newCode.length == 20){
                    if(this.type == '1'){
                        // 扫码入库
                        this.$modal.loading("正在加载")
                        scanCode(this.scanCodeData).then(res => {
                            if(res.code == 200){
                                this.$modal.closeLoading()
                                this.getDetailData()
                                this.scanCodeData.code = ''
                                this.$refs.getFocus.focus()
                                this.$modal.msgSuccess("扫码成功")
                                if(this.morePositionType == '1'){
                                    morePosition(this.scanCodeData.otherInboundDetailId).then(res => {
                                        res.rows.forEach(i => {
                                            if(i.endNum == 0 || i.endNum == null){
                                                i.endNum = ''
                                            }
                                        })
                                        this.moreData = res.rows
                                    })
                                }
                            }else{
                                this.$modal.closeLoading()
                                this.scanCodeData.code = ''
                                this.$refs.getFocus.focus()
                                this.$modal.msgError(res.msg)
                            }
                        })
                    }else{
                        // 删除扫码
                        this.$modal.loading("正在加载")
                        scanCodeDelete(this.scanCodeData).then(res => {
                            if(res.code == 200){
                                this.$modal.closeLoading()
                                this.getDetailData()
                                this.scanCodeData.code = ''
                                this.$refs.getFocus.focus()
                                this.$modal.msgSuccess("删除成功")
                                if(this.morePositionType == '1'){
                                    morePosition(this.scanCodeData.otherInboundDetailId).then(res => {
                                        res.rows.forEach(i => {
                                            if(i.endNum == 0 || i.endNum == null){
                                                i.endNum = ''
                                            }
                                        })
                                        this.moreData = res.rows
                                    })
                                }
                            }else{
                                this.$modal.closeLoading()
                                this.scanCodeData.code = ''
                                this.$refs.getFocus.focus()
                                this.$modal.msgError(res.msg)
                            }
                        })
                    }
                }else{
                    this.scanCodeData.code = ''
                    this.$refs.getFocus.focus()
                    this.$modal.msgError('扫码错误，请重新扫码!')
                }
            }else{
                // 直接按键盘触发
                // console.log(this.scanCodeData.code)
                let newCode = this.common.codeRule(this.scanCodeData.code)
                // let code = this.scanCodeData.code.split("=")
                // let newCode = code[code.length-1]
                this.scanCodeData.code = newCode
                if(newCode.length == 13 || newCode.length == 20){
                    if(this.type == '1'){
                        // 扫码入库
                        this.$modal.loading("正在加载")
                        scanCode(this.scanCodeData).then(res => {
                            if(res.code == 200){
                                this.$modal.closeLoading()
                                this.getDetailData()
                                this.scanCodeData.code = ''
                                this.$refs.getFocus.focus()
                                this.$modal.msgSuccess("扫码成功")
                                if(this.morePositionType == '1'){
                                    morePosition(this.scanCodeData.otherInboundDetailId).then(res => {
                                        res.rows.forEach(i => {
                                            if(i.endNum == 0 || i.endNum == null){
                                                i.endNum = ''
                                            }
                                        })
                                        this.moreData = res.rows
                                    })
                                }
                            }else{
                                this.$modal.closeLoading()
                                this.scanCodeData.code = ''
                                this.$refs.getFocus.focus()
                                this.$modal.msgError(res.msg)
                            }
                        })
                    }else{
                        // 删除扫码
                        console.log(this.scanCodeData)
                        this.$modal.loading("正在加载")
                        scanCodeDelete(this.scanCodeData).then(res => {
                            if(res.code == 200){
                                this.$modal.closeLoading()
                                this.getDetailData()
                                this.scanCodeData.code = ''
                                this.$refs.getFocus.focus()
                                this.$modal.msgSuccess("删除成功")
                                if(this.morePositionType == '1'){
                                    console.log(this.scanCodeData)
                                    morePosition(this.scanCodeData.otherInboundDetailId).then(res => {
                                        res.rows.forEach(i => {
                                            if(i.endNum == 0 || i.endNum == null){
                                                i.endNum = ''
                                            }
                                        })
                                        this.moreData = res.rows
                                    })
                                }
                            }else{
                                this.$modal.closeLoading()
                                this.scanCodeData.code = ''
                                this.$refs.getFocus.focus()
                                this.$modal.msgError(res.msg)
                            }
                        })
                    }
                }else{
                    this.scanCodeData.code = ''
                    this.$refs.getFocus.focus()
                    this.$modal.msgError('扫码错误，请重新扫码!')
                }
            }
        },
        // 多仓位
        showMorePosition(row){
            if(row.warehouse !== null && row.warehouse !== ''){
                this.closeScan = false
                this.scanCodeData = {}
                // 当前选中仓库对应仓库列表的数据
                let isWarehouse = this.warehouseList.find(item => item.warehouseId == row.warehouse)
                // 判断是否启用仓位管理
                if(isWarehouse.openPosition == '0'){
                    if(row.areaName !== null && row.areaName !== ''){
                        morePosition(row.id).then(res => {
                            if(res.code == 200){
                                if(res.rows.length > 0 && row.endNum > 0){
                                    this.morePosition = true
                                    res.rows.forEach(i => {
                                        if(i.endNum == 0 || i.endNum == null){
                                            i.endNum = ''
                                        }
                                    })
                                    this.moreData = res.rows
                                }else{
                                    if(row.status == 0){
                                        this.morePosition = true
                                        this.morePositionData = row
                                        if(res.rows.length == 0 && row.endNum > 0){
                                            let obj = {
                                                businessType: 2,
                                                scanCode: row.scanCode,
                                                businessId: row.id, // 采购单详情id
                                                productId: row.productId, // 产品id
                                                productCode: row.productCode, // 产品编码
                                                productName: row.productName, // 产品名称
                                                endNum: row.endNum, // 实际数量
                                                warehouse: row.warehouse, // 仓库
                                                areaId: row.areaId, // 区域id
                                                areaName: row.areaName, // 区域名称
                                                positionId: row.positionId, // 仓位id
                                                positionName: row.positionName, // 仓位名称
                                            }
                                            this.getMorePositionAdd(obj)
                                        }else if(res.rows.length >= 0 && row.endNum == '' ){
                                            let obj = {
                                                businessType: 2,
                                                scanCode: row.scanCode,
                                                businessId: row.id, // 采购单详情id
                                                productId: row.productId, // 产品id
                                                productCode: row.productCode, // 产品编码
                                                productName: row.productName, // 产品名称
                                                endNum: '', // 实际数量
                                                warehouse: row.warehouse, // 仓库
                                                areaId: row.areaId, // 区域id
                                                areaName: row.areaName, // 区域名称
                                                positionId: row.positionId, // 仓位id
                                                positionName: row.positionName, // 仓位名称
                                            }
                                            this.getMorePositionAdd(obj)
                                        }else{
                                            res.rows.forEach(i => {
                                                if(i.endNum == 0 || i.endNum == null){
                                                    i.endNum = ''
                                                }
                                            })
                                            this.moreData = res.rows
                                        }
                                    }
                                }
                            }
                        })
                    }else{
                        this.$modal.msgError('请选择仓位！')
                    }
                }else{
                    this.$modal.msgError('未启用仓位管理！')
                }
            }else{
                this.$modal.msgError('请选择仓库！')
            }
        },
        // 多仓位新建数据
        getMorePositionAdd(e){
            morePositionAdd(e).then(res => {
                if(res.code == 200){
                    morePosition(e.businessId).then(res => {
                        res.rows.forEach(i => {
                            if(i.endNum == 0 || i.endNum == null){
                                i.endNum = ''
                            }
                        })
                        this.moreData = res.rows
                    })
                }
            })
        },
        // 多仓位新建仓位
        onMorePositionAdd(row){
            if(row.status == '0'){
                let obj = {
                    businessType: 2,
                    scanCode: row.scanCode,
                    businessId: row.businessId, // 采购单详情id
                    productId: row.productId, // 产品id
                    productCode: row.productCode, // 产品编码
                    productName: row.productName, // 产品名称
                    purchaseNum: row.purchaseNum, // 订单数量
                    endNum: '', // 实际数量
                    warehouse: row.warehouse, // 仓库
                    areaId: '', // 区域id
                    areaName: '', // 区域名称
                    positionId: '', // 仓位id
                    positionName: '', // 仓位名称
                }
                morePositionAddSingle(obj).then(res => {
                    if(res.code == 200){
                        morePosition(row.businessId).then(res => {
                            res.rows.forEach(i => {
                                if(i.endNum == 0 || i.endNum == null){
                                    i.endNum = ''
                                }
                            })
                            this.moreData = res.rows
                        })
                    }
                })
            }
        },
        // 多仓位实际数量验证
        inputEditNum(val, row){
            // console.log(row)
            if(row.areaId !== null ){
                if(val == '' || val == undefined){
                    this.$modal.msgError('实际数量不能为空!')
                }else{
                    if(/^[1-9]\d*$|^0$/.test(val) == false){
                        this.$modal.msgError('实际数量必须是数字！')
                        row.endNum = ''
                        return false
                    }else{
                        if(JSON.parse(val) <= 0){
                            row.endNum = ''
                            this.$modal.msgError('实际数量必须大于0!')
                        }
                    }
                }
            }else{
                row.endNum = ''
                this.$modal.msgError('请选择仓位!')
            }
        },
        // 多仓位数据保存
        onBlur(val, row){
            if(val.target.value == null || val.target.value == ''){
                this.$modal.msgError('实际数量不能为空!')
            }else{
                morePositionEdit(row).then(res => {
                    if(res.code == 200){
                        this.getDetailData()
                    }
                })
            }
        },
        // 多仓位跳转仓库列表页面
        morePositionWarehouseList(row, index){
            if(row.status == '0'){
                if(row.endNum > 0){
                    this.$modal.msgError('实际数量已存在，不能编辑!')
                }else{
                    if(this.$route.query.id){
                        this.$router.push({ path:"/warehouseManagement/warehouse/list", query:{ id: this.$route.query.id, type: 'otherStorageMorePosition', index: index, list: row, form: this.form }})
                    }else{
                        this.$router.push({ path:"/warehouseManagement/warehouse/list", query:{ type: 'otherStorageMorePosition', index: index, list: row, form: this.form}})
                    }
                }
            }
        },
        // 多仓位删除仓位
        onMorePositionDelete(row){
            if(row.status == '0'){
                this.$modal.confirm('是否确认删除当前数据？', '删除').then(() => {
                    morePositionDeleteSingle(row.id).then(res => {
                        if(res.code == 200){
                            morePosition(row.businessId).then(res => {
                                res.rows.forEach(i => {
                                    if(i.endNum == 0 || i.endNum == null){
                                        i.endNum = ''
                                    }
                                })
                                this.moreData = res.rows
                            })
                        }
                    })
                }).catch(() => {})
            }
        },
        // 关闭多仓位
        onHideMorePosition(){
            this.morePosition = false
            this.closeScanMore = false
            this.getDetailData()
        },
        // 添加一条产品信息
        addProduct(){
            let data = {
                key: {
                    otherInboundOrderId: this.id ? this.id : '',
                    productId: ''
                }
            }
            this.columns.forEach(item =>{
                data.key[item.prop] = ''
            })
            this.productData.push(JSON.parse(JSON.stringify(data.key)))
        },
        // 删除数据
        deleteRow(item){
            if(item.status == '0'){
                if(item.productId !== ''){
                    this.$modal.confirm('是否确认删除当前数据？', '删除').then(() => {
                        otherStorageDelete(item.id).then(res => {
                            if(res.code == 200){
                                this.getDetailData()
                            }
                        })
                    }).catch(() => {})
                }else{
                    this.$modal.msgError('最少保留一条可编辑数据！')
                }
            }
        },
        // 保存数据
        onChangeSave(){
            this.getOtherStorage()
        },
        onBlurSave(){
            this.getOtherStorage()
        },
        // 新建数据
        getOtherStorage(){
            let data = this.productData.filter(item => item.productCode !== '')
            this.form.detailList = data
            // console.log(this.id)
            if(this.id || this.$route.query.id){
                let arr = this.form
                arr.id = this.id ? this.id : this.$route.query.id
                otherStorageEdit(arr).then(res => {
                    if(res.code == 200){
                        this.getDetailData()
                        // this.$modal.msgSuccess("操作成功")
                    }
                })
            }else{
                // 新建
                otherStorageAdd(this.form).then(res=> {
                    if(res.code == 200){
                        this.id = res.data.id
                        this.getDetailData()
                        // this.$modal.msgSuccess("操作成功")
                    }
                })
            }
        },
        // 确定入库
        submitForm(){
            // 过滤掉空数据
            let arr = this.productData.filter(item => item.productCode !== '')
            this.form.detailList = arr
            let enableBatches = this.form.detailList.every(val => {
                if(val.enableBatches == '0'){
                    return !!val.batchNumber
                }else{
                    return !val.batchNumber
                }
            })
            let endNum = this.form.detailList.every(val => !!val.endNum)
            if(enableBatches == true){
                if(endNum == true){
                    this.$modal.loading("正在提交")
                    confirmStorage(this.form).then(res => {
                        if(res.code == 200){
                            this.$modal.msgSuccess("操作成功")
                            this.$tab.closeOpenPage({ path: "/warehouseManagement/warehouse/otherStorage" })
                            this.$modal.closeLoading()
                        }
                    })
                }else{
                    this.$modal.msgError('入库数量不能为空！')
                }
            }else{
                this.$modal.msgError('批次号不能为空！')
            }
        }
    }
}
</script>

<style scoped>
::v-deep .el-input .el-input-group__append{
  padding: 0 10px;
}
.del{
  color: #ff0000;
  cursor: pointer;
}
.more{
  font-size: 30px;
  cursor: pointer;
}
::v-deep .el-dialog__header{
  display: none;
}
.icon-more-position{
  font-size: 22px;
  margin: 0 5px;
  cursor: pointer;
}
</style>